package com.guigu.common;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用于拦截所有请求（排除用户的注册,登录），判断用户是否登录
 */
public class AuthorizationInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        System.out.println(uri);

        HttpSession session = request.getSession();
        Cookie[] cookies = request.getCookies();
        Object user = session.getAttribute("user");
        if(user != null) {
            return true;
        }
        response.sendRedirect("/erp/Login");
        return false;
    }
}
